﻿using System;
using System.Collections.Generic;
using System.Xml;
using Autodesk.Revit.DB;
using SystemDiagramNew.SystemDiagramLegend.DataDefine;
using SystemDiagramNew.TypeDefinition;

namespace SystemDiagramNew.SystemDiagramLegend
{
	public class XMLKit
	{
		public static List<LegendGroup> ReadLegendXMLFile(string fileName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(fileName);
			List<LegendGroup> list = new List<LegendGroup>();
			XmlNode xmlNode = xmlDocument.SelectSingleNode("LegendGroupArray");
			if (xmlNode == null)
			{
				return list;
			}
			XmlNodeList childNodes = xmlNode.ChildNodes;
			foreach (object obj in childNodes)
			{
				XmlNode legendGroupNode = (XmlNode)obj;
				LegendGroup legendGroup = XMLKit.ReadLegendGroup(legendGroupNode);
				if (legendGroup != null)
				{
					list.Add(legendGroup);
				}
			}
			return list;
		}

		public static bool AddLegendInfoToLegendFile(string fileName, LegendGroup legendGroup)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(fileName);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("LegendGroupArray");
			if (xmlNode == null)
			{
				return false;
			}
			XmlNodeList childNodes = xmlNode.ChildNodes;
			for (int i = 0; i < childNodes.Count; i++)
			{
				string strB = XMLKit.ReadStringSubNode(childNodes.Item(i), "Name");
				if (legendGroup.TheName.CompareTo(strB) == 0)
				{
					xmlNode.RemoveChild(childNodes.Item(i));
					break;
				}
			}
			XmlNode newChild = XMLKit.WriteLegendGroup(xmlDocument, legendGroup);
			xmlNode.AppendChild(newChild);
			xmlDocument.Save(fileName);
			return true;
		}

		public static bool DeleteLegendInfoFromLegendFile(string fileName, LegendGroup legendGroup)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(fileName);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("LegendGroupArray");
			if (xmlNode == null)
			{
				return false;
			}
			XmlNodeList childNodes = xmlNode.ChildNodes;
			for (int i = 0; i < childNodes.Count; i++)
			{
				string strB = XMLKit.ReadStringSubNode(childNodes.Item(i), "Name");
				if (legendGroup.TheName.CompareTo(strB) == 0)
				{
					xmlNode.RemoveChild(childNodes.Item(i));
					break;
				}
			}
			xmlDocument.Save(fileName);
			return true;
		}

		public static bool RenameLegendInfoToLegendFile(string fileName, string oldName, LegendGroup legendGroup)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(fileName);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("LegendGroupArray");
			if (xmlNode == null)
			{
				return false;
			}
			XmlNodeList childNodes = xmlNode.ChildNodes;
			for (int i = 0; i < childNodes.Count; i++)
			{
				string strB = XMLKit.ReadStringSubNode(childNodes.Item(i), "Name");
				if (oldName.CompareTo(strB) == 0)
				{
					xmlNode.RemoveChild(childNodes.Item(i));
					break;
				}
			}
			XmlNode newChild = XMLKit.WriteLegendGroup(xmlDocument, legendGroup);
			xmlNode.AppendChild(newChild);
			xmlDocument.Save(fileName);
			return true;
		}

		public static List<MappingData> ReadMappingFile(string fileName)
		{
			List<MappingData> list = new List<MappingData>();
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				xmlDocument.Load(fileName);
			}
			catch (Exception)
			{
				return list;
			}
			XmlNode xmlNode = xmlDocument.SelectSingleNode("MappingRoot");
			if (xmlNode == null)
			{
				return list;
			}
			XmlNodeList childNodes = xmlNode.ChildNodes;
			foreach (object obj in childNodes)
			{
				XmlNode xmlNode2 = (XmlNode)obj;
				XmlElement xmlElement = xmlNode2 as XmlElement;
				string attribute = xmlElement.GetAttribute("FamilyName");
				string attribute2 = xmlElement.GetAttribute("LegendName");
				MappingData item = new MappingData(attribute, attribute2);
				list.Add(item);
			}
			return list;
		}

		public static void WriteMappingFile(string fileName, List<MappingData> mappingDataList)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode xmlNode = xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, "", "");
			XmlNode xmlNode2 = xmlNode;
			xmlNode2.InnerText += " encoding=\"GB2312\"";
			xmlDocument.AppendChild(xmlNode);
			XmlElement xmlElement = xmlDocument.CreateElement("", "MappingRoot", "");
			xmlDocument.AppendChild(xmlElement);
			foreach (MappingData mappingData in mappingDataList)
			{
				XmlElement xmlElement2 = xmlDocument.CreateElement("Mapping");
				xmlElement2.SetAttribute("FamilyName", mappingData.FamilyName);
				xmlElement2.SetAttribute("LegendName", mappingData.LegendName);
				xmlElement.AppendChild(xmlElement2);
			}
			try
			{
				xmlDocument.Save(fileName);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}

		private static LegendGroup ReadLegendGroup(XmlNode legendGroupNode)
		{
			string name = XMLKit.ReadStringSubNode(legendGroupNode, "Name");
			int legendKind = XMLKit.ReadIntSubNode(legendGroupNode, "Kind");
			Legend leftLink = XMLKit.ReadLegend(legendGroupNode, "Legend_Left");
			Legend rightLink = XMLKit.ReadLegend(legendGroupNode, "Legend_Right");
			Legend topLink = XMLKit.ReadLegend(legendGroupNode, "Legend_Top");
			Legend bottomLink = XMLKit.ReadLegend(legendGroupNode, "Legend_Bottom");
			return new LegendGroup(name, (LegendKind)legendKind, leftLink, rightLink, topLink, bottomLink);
		}

		private static Legend ReadLegend(XmlNode legendGroupNode, string name)
		{
			XmlNode xmlNode = legendGroupNode.SelectSingleNode(name);
			if (xmlNode == null)
			{
				return null;
			}
			XYZ position = XMLKit.ReadPointSubNode(xmlNode, "Position");
			XmlNode xmlNode2 = xmlNode.SelectSingleNode("ShapArray");
			if (xmlNode2 == null)
			{
				return null;
			}
			List<TextNoteInfo> list = new List<TextNoteInfo>();
			List<CurveInfo> list2 = new List<CurveInfo>();
			List<FillRegion> list3 = new List<FillRegion>();
			XmlNodeList childNodes = xmlNode2.ChildNodes;
			foreach (object obj in childNodes)
			{
				XmlNode xmlNode3 = (XmlNode)obj;
				if (xmlNode3.LocalName.CompareTo("Curve") == 0)
				{
					CurveInfo item = XMLKit.ReadCurveSubNode(xmlNode3);
					if (!item.IsNull())
					{
						list2.Add(item);
					}
				}
				else if (xmlNode3.LocalName.CompareTo("Text") == 0)
				{
					TextNoteInfo item2 = XMLKit.ReadTextNoteSubNode(xmlNode3);
					list.Add(item2);
				}
				else
				{
					FillRegion item3 = XMLKit.ReadFillRegionSubNode(xmlNode3);
					list3.Add(item3);
				}
			}
			return new Legend(position, list, list2, list3);
		}

		private static EdgeInfo ReadEdgeSubNode(XmlNode node)
		{
			XYZ startPoint = XMLKit.ReadPointSubNode(node, "StartPoint");
			XYZ endPoint = XMLKit.ReadPointSubNode(node, "EndPoint");
			double bulge = XMLKit.ReadDoubleSubNode(node, "Bugle");
			return new EdgeInfo(startPoint, endPoint, bulge);
		}

		private static CurveInfo ReadCurveSubNode(XmlNode node)
		{
			EdgeInfo curve = XMLKit.ReadEdgeSubNode(node);
			CurveInfo result = new CurveInfo(curve, PenColor.PC_Black, 1f);
			return result;
		}

		private static FillRegion ReadFillRegionSubNode(XmlNode node)
		{
			List<EdgeInfo> list = new List<EdgeInfo>();
			XmlNodeList childNodes = node.ChildNodes;
			foreach (object obj in childNodes)
			{
				XmlNode node2 = (XmlNode)obj;
				EdgeInfo edgeInfo = XMLKit.ReadEdgeSubNode(node2);
				if (edgeInfo != null)
				{
					list.Add(edgeInfo);
				}
			}
			FillRegion result = new FillRegion(list, PenColor.PC_Black);
			return result;
		}

		private static TextNoteInfo ReadTextNoteSubNode(XmlNode node)
		{
			string text = XMLKit.ReadStringSubNode(node, "String");
			XYZ center = XMLKit.ReadPointSubNode(node, "Position");
			double boundingBoxWidth = XMLKit.ReadDoubleSubNode(node, "Width");
			double num = XMLKit.ReadDoubleSubNode(node, "Height");
			TextNoteInfo result = new TextNoteInfo(text, PenColor.PC_Black, num, center, boundingBoxWidth, num);
			return result;
		}

		private static XYZ ReadPointSubNode(XmlNode node, string name)
		{
			XmlNode xmlNode = node.SelectSingleNode(name);
			if (xmlNode == null)
			{
				return XYZ.Zero;
			}
			double num = XMLKit.ReadDoubleSubNode(xmlNode, "X");
			double num2 = XMLKit.ReadDoubleSubNode(xmlNode, "Y");
			double num3 = XMLKit.ReadDoubleSubNode(xmlNode, "Z");
			return new XYZ(num, num2, num3);
		}

		private static string ReadStringSubNode(XmlNode node, string name)
		{
			string result = "";
			XmlNode xmlNode = node.SelectSingleNode(name);
			if (xmlNode == null)
			{
				return result;
			}
			if (xmlNode.InnerText.CompareTo("") != 0)
			{
				result = xmlNode.InnerText;
			}
			return result;
		}

		private static int ReadIntSubNode(XmlNode node, string name)
		{
			string value = XMLKit.ReadStringSubNode(node, name);
			int result;
			try
			{
				int num = Convert.ToInt32(value);
				result = num;
			}
			catch (Exception)
			{
				result = 0;
			}
			return result;
		}

		private static double ReadDoubleSubNode(XmlNode node, string name)
		{
			string value = XMLKit.ReadStringSubNode(node, name);
			double result;
			try
			{
				double num = Convert.ToDouble(value);
				result = num;
			}
			catch (Exception)
			{
				result = 0.0;
			}
			return result;
		}

		private static XmlNode WriteLegendGroup(XmlDocument xmlDoc, LegendGroup legendGroup)
		{
			string name = "LegendGroup";
			XmlNode xmlNode = xmlDoc.CreateElement(name);
			XmlNode newChild = XMLKit.NewElement(xmlDoc, "Name", legendGroup.TheName);
			xmlNode.AppendChild(newChild);
			XmlNode newChild2 = XMLKit.NewElement(xmlDoc, "Kind", (int)legendGroup.TheLegendKind);
			xmlNode.AppendChild(newChild2);
			XmlNode newChild3 = XMLKit.ExporterLegend(xmlDoc, legendGroup.LeftLink, "Legend_Left");
			xmlNode.AppendChild(newChild3);
			XmlNode newChild4 = XMLKit.ExporterLegend(xmlDoc, legendGroup.RightLink, "Legend_Right");
			xmlNode.AppendChild(newChild4);
			XmlNode newChild5 = XMLKit.ExporterLegend(xmlDoc, legendGroup.TopLink, "Legend_Top");
			xmlNode.AppendChild(newChild5);
			XmlNode newChild6 = XMLKit.ExporterLegend(xmlDoc, legendGroup.BottomLink, "Legend_Bottom");
			xmlNode.AppendChild(newChild6);
			return xmlNode;
		}

		private static XmlNode ExporterLegend(XmlDocument xmlDoc, Legend legend, string nodeName)
		{
			XmlNode xmlNode = xmlDoc.CreateElement(nodeName);
			XmlNode newChild = XMLKit.NewElement(xmlDoc, "Position", legend.Position);
			xmlNode.AppendChild(newChild);
			XmlNode xmlNode2 = xmlDoc.CreateElement("ShapArray");
			xmlNode.AppendChild(xmlNode2);
			List<CurveInfo> curveInfoList = legend.CurveInfoList;
			for (int i = 0; i < curveInfoList.Count; i++)
			{
				CurveInfo curve = curveInfoList[i];
				XmlNode xmlNode3 = XMLKit.ExporterCurve(xmlDoc, curve);
				if (xmlNode3 != null)
				{
					xmlNode2.AppendChild(xmlNode3);
				}
			}
			List<TextNoteInfo> textNoteInfoList = legend.TextNoteInfoList;
			for (int i = 0; i < textNoteInfoList.Count; i++)
			{
				TextNoteInfo text = textNoteInfoList[i];
				XmlNode xmlNode4 = XMLKit.ExporterText(xmlDoc, text);
				if (xmlNode4 != null)
				{
					xmlNode2.AppendChild(xmlNode4);
				}
			}
			List<FillRegion> fillRegionList = legend.FillRegionList;
			for (int i = 0; i < fillRegionList.Count; i++)
			{
				FillRegion pFillRegion = fillRegionList[i];
				XmlNode xmlNode5 = XMLKit.ExporterFillRegion(xmlDoc, pFillRegion);
				if (xmlNode5 != null)
				{
					xmlNode2.AppendChild(xmlNode5);
				}
			}
			return xmlNode;
		}

		private static XmlNode ExporterCurve(XmlDocument xmlDoc, CurveInfo curve)
		{
			XmlNode xmlNode = xmlDoc.CreateElement("Curve");
			XmlNode newChild = XMLKit.NewElement(xmlDoc, "StartPoint", curve.CurveObj.StartPoint);
			xmlNode.AppendChild(newChild);
			XmlNode newChild2 = XMLKit.NewElement(xmlDoc, "EndPoint", curve.CurveObj.EndPoint);
			xmlNode.AppendChild(newChild2);
			XmlNode newChild3 = XMLKit.NewElement(xmlDoc, "Bugle", curve.CurveObj.Bulge);
			xmlNode.AppendChild(newChild3);
			return xmlNode;
		}

		private static XmlNode ExporterText(XmlDocument xmlDoc, TextNoteInfo text)
		{
			XmlNode xmlNode = xmlDoc.CreateElement("Text");
			XmlNode newChild = XMLKit.NewElement(xmlDoc, "String", text.m_Text);
			xmlNode.AppendChild(newChild);
			XmlNode newChild2 = XMLKit.NewElement(xmlDoc, "Position", text.m_Center);
			xmlNode.AppendChild(newChild2);
			XmlNode newChild3 = XMLKit.NewElement(xmlDoc, "Width", text.m_BoundingBoxWidth);
			xmlNode.AppendChild(newChild3);
			XmlNode newChild4 = XMLKit.NewElement(xmlDoc, "Height", text.m_BoundingBoxHeight);
			xmlNode.AppendChild(newChild4);
			return xmlNode;
		}

		private static XmlNode ExporterFillRegion(XmlDocument xmlDoc, FillRegion pFillRegion)
		{
			XmlNode xmlNode = xmlDoc.CreateElement("Region");
			List<EdgeInfo> edgeLoop = pFillRegion.EdgeLoop;
			for (int i = 0; i < edgeLoop.Count; i++)
			{
				XmlNode xmlNode2 = xmlDoc.CreateElement("Edge");
				EdgeInfo edgeInfo = edgeLoop[i];
				XmlNode newChild = XMLKit.NewElement(xmlDoc, "StartPoint", edgeInfo.StartPoint);
				xmlNode2.AppendChild(newChild);
				XmlNode newChild2 = XMLKit.NewElement(xmlDoc, "EndPoint", edgeInfo.EndPoint);
				xmlNode2.AppendChild(newChild2);
				XmlNode newChild3 = XMLKit.NewElement(xmlDoc, "Bugle", edgeInfo.Bulge);
				xmlNode2.AppendChild(newChild3);
				xmlNode.AppendChild(xmlNode2);
			}
			return xmlNode;
		}

		private static XmlNode NewElement(XmlDocument xmlDoc, string elementName, string putText)
		{
			XmlNode xmlNode = xmlDoc.CreateElement(elementName);
			xmlNode.InnerText = putText;
			return xmlNode;
		}

		private static XmlNode NewElement(XmlDocument xmlDoc, string elementName, int putValue)
		{
			string putText = putValue.ToString();
			return XMLKit.NewElement(xmlDoc, elementName, putText);
		}

		private static XmlNode NewElement(XmlDocument xmlDoc, string elementName, double putValue)
		{
			string putText = putValue.ToString();
			return XMLKit.NewElement(xmlDoc, elementName, putText);
		}

		private static XmlNode NewElement(XmlDocument xmlDoc, string elementName, XYZ pt)
		{
			XmlNode xmlNode = xmlDoc.CreateElement(elementName);
			XmlNode newChild = XMLKit.NewElement(xmlDoc, "X", pt.X);
			xmlNode.AppendChild(newChild);
			XmlNode newChild2 = XMLKit.NewElement(xmlDoc, "Y", pt.Y);
			xmlNode.AppendChild(newChild2);
			XmlNode newChild3 = XMLKit.NewElement(xmlDoc, "Z", pt.Z);
			xmlNode.AppendChild(newChild3);
			return xmlNode;
		}
	}
}
